﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.ComponentModel;
using rs = ARR.Shared;
using System.Linq.Expressions;
using System.Text.RegularExpressions;
using System.Collections.ObjectModel;
using System.Windows;
using System.Reflection;
using System.ComponentModel.DataAnnotations;


namespace ARR.UI.ViewModels
{
    public abstract class ViewModelBase : INotifyPropertyChanged, IDisposable
    {

        private string icon;



        protected ViewModelBase()
        {
        }



        public virtual string DisplayName { get; protected set; }

        public virtual string Icon
        {
            get
            {
                if (icon == null)
                    return "/Images/Columnchart-20.png"; 
                
                return icon;
            }
            protected set
            {
                icon = value;
            }
        }

        public virtual bool IsDirty
        {
            get;
            set;
        }

        public virtual List<string> ValidationErrors
        {
            get
            {
                return null;
            }
            set{}
        }

        public virtual Visibility IsVisibleValidationSummary
        {
            get
            {
                return (ValidationErrors != null && ValidationErrors.Any()) ? Visibility.Visible : Visibility.Collapsed;
            }
        }



        public event PropertyChangedEventHandler PropertyChanged;





        public void Dispose()
        {
            this.OnDispose();
        }

        public void NotiftyOnClosing(object sender, EventArgs e)
        {
            this.OnClosing(sender, e);
        }

        public static string PropertyName<T>(Expression<Func<T, object>> propertyExp)
        {
            return Regex.Match(propertyExp.Body.ToString(), @"\b\w+?\.(.+?)\b").Groups[1].Value;
        }


        protected virtual void OnClosing(object sender, EventArgs e) { }

        protected virtual void OnDispose()
        {
        }

        protected virtual void OnPropertyChanged<T>(Expression<Func<T, object>> propertyExp) where T : ViewModelBase
        {
            string propertyName = ViewModelBase.PropertyName(propertyExp);
            this.VerifyPropertyName(propertyName);

            PropertyChangedEventHandler handler = this.PropertyChanged;
            if (handler != null)
            {
                PropertyChangedEventArgs e = new PropertyChangedEventArgs(propertyName);
                handler(this, e);
            }
        }


        [Conditional("DEBUG")]
        [DebuggerStepThrough]
        private void VerifyPropertyName(string propertyName)
        {
            // Verify that the property name matches a real,  
            // public, instance property on this object.
            if (TypeDescriptor.GetProperties(this)[propertyName] == null)
            {
                throw new ArgumentException(string.Format(rs::Errors.InvalidPropertyName, propertyName), "propertyName");
            }
        }



#if DEBUG
        ~ViewModelBase()
        {
            string msg = string.Format("{0} ({1}) Finalized", this.GetType().Name, this.GetHashCode());
            System.Diagnostics.Debug.WriteLine(msg);
        }
#endif
    }
}
